/*
 * Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package jdk.graal.compiler.core.test;

import org.junit.Test;

import jdk.graal.compiler.api.directives.GraalDirectives;
import jdk.graal.compiler.core.common.type.StampFactory;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.nodeinfo.NodeCycles;
import jdk.graal.compiler.nodeinfo.NodeInfo;
import jdk.graal.compiler.nodeinfo.NodeSize;
import jdk.graal.compiler.nodes.AbstractBeginNode;
import jdk.graal.compiler.nodes.BeginNode;
import jdk.graal.compiler.nodes.FixedNode;
import jdk.graal.compiler.nodes.IfNode;
import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.graal.compiler.nodes.ValueNode;
import jdk.graal.compiler.nodes.debug.SideEffectNode;
import jdk.graal.compiler.nodes.memory.FloatingReadNode;
import jdk.graal.compiler.phases.graph.ScheduledNodeIterator;

/**
 * Test to verify that the {@link ScheduledNodeIterator} can handle cases where
 * {@link AbstractBeginNode} have data inputs that can be lowered.
 */
public class ScheduledNodeIteratorBeginTest extends GraalCompilerTest {

    static class A {
        int x;
    }

    public static int snippet(A a, int x) {
        int res = a.x;
        if (x > 10) {
            GraalDirectives.sideEffect(2);
        } else {
            GraalDirectives.sideEffect(1 + res);
        }
        GraalDirectives.sideEffect(3);
        return x;
    }

    /**
     * Note that we piggy back necessary graph modifications to expose the problematic pattern in
     * checkMidTierGraph here.
     */
    @Override
    protected void checkMidTierGraph(StructuredGraph graph) {
        super.checkMidTierGraph(graph);
        assert graph.getNodes().filter(FloatingReadNode.class).count() == 1;

        // the one with the higher node ID
        IfNode ifNode = null;

        for (IfNode i : graph.getNodes(IfNode.TYPE)) {
            if (i.trueSuccessor().next() instanceof SideEffectNode) {
                assert ifNode == null : "Must not find multiple if nodes";
                ifNode = i;
                break;
            }
        }
        assert ifNode != null;

        BeginNode trueSucc = (BeginNode) ifNode.trueSuccessor();

        BeginWithReadUsage b = new BeginWithReadUsage(graph.getNodes().filter(FloatingReadNode.class).first());
        b = graph.add(b);

        ifNode.setTrueSuccessor(null);

        FixedNode oldNext = trueSucc.next();
        trueSucc.setNext(null);

        b.setNext(oldNext);

        ifNode.setTrueSuccessor(b);

        trueSucc.safeDelete();
    }

    @Test
    public void test() {
        test("snippet", new A(), 123);
    }

    @NodeInfo(cycles = NodeCycles.CYCLES_UNKNOWN, cyclesRationale = "Test", size = NodeSize.SIZE_UNKNOWN, sizeRationale = "Test")
    public static class BeginWithReadUsage extends AbstractBeginNode {

        public static final NodeClass<BeginWithReadUsage> TYPE = NodeClass.create(BeginWithReadUsage.class);
        @Input protected ValueNode input;

        @SuppressWarnings("this-escape")
        public BeginWithReadUsage(ValueNode input) {
            super(TYPE, StampFactory.forVoid());
            this.input = input;
        }
    }

}
